package com.zhongjianjian.RabbitMq.config;

import com.rabbitmq.client.ConnectionFactory;
import org.apache.ibatis.annotations.Param;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * @类名: com.zhongjianjian.RabbitMq.config
 * @作者: lgt
 * @创建时间: 2020-03-02 21:09
 * @描述:
 **/
@Configuration
public class RabbitConfig {

    /*@Bean("rabbitTemplate2")
    public RabbitTemplate getRabbitTemplate(ConnectionFactory connectionFactory){
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        return rabbitTemplate;
    }*/

    //设置死信队列
    @Bean("deadQueue")
    public Queue getDeadQueue(){
        return new Queue("TEST-DEAD-QUEUE",true,false,false);
    }

    //设置死信交换机
    @Bean("deadExchange")
    public Exchange getDeadExchange(){
        return ExchangeBuilder.directExchange("TEST-DEAD-EXCHANGE").durable(true).build();
    }

    //绑定死信交换机和死信队列
    @Bean
    public Binding getDeadBinding(Queue deadQueue, Exchange deadExchange){
        return BindingBuilder.bind(deadQueue).to(deadExchange).with("DEAD-KEY").noargs();
    }

    //设置正常队列
    @Bean("testQueue")
    public Queue getQueue(){
        //给正常的队列绑定死信队列
        Map<String, Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange","TEST-DEAD-EXCHANGE");//绑定死信交换器
        map.put("x-dead-letter-routing-key","DEAD-KEY");
        return new Queue("TEST-QUEUE",true,false,false,map);
    }

    //设置正常交换机
    @Bean("testExchange")
    public Exchange getExchange(){
        return ExchangeBuilder.directExchange("TEST-EXCHANGE").durable(true).build();
    }

    //设置绑定
    @Bean
    public Binding getBinding(Exchange testExchange,Queue testQueue){
        return BindingBuilder.bind(testQueue).to(testExchange).with("TEST-KEY").noargs();
    }
}
